package com.hoyo.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hoyo.domain.User;
import com.hoyo.domain.UserInfo;
import com.hoyo.domain.UserPermission;

@Mapper
public interface UserMapper {

	@Select("select * from user where is_locked=false and (phone=#{identifier} or email=#{identifier}) limit 1")
	User findByIdentifier(String identifier) throws Exception;
	
	@Select("select * from user where id=#{0}")
	User findById(int id) throws Exception;
	
	@Select("select * from user where email=#{email} limit 1")
	User findUserByEmail(String email) throws Exception;

	@Select("select * from user where phone=#{telephone} limit 1")
	User findUserByTelephone(String telephone) throws Exception;
	
	@Update("update user set password=#{0} where id=#{1}")
	void modifyPasswordByUserId(String password, int userId) throws Exception;
	
	@Update("update user set phone=#{0} where id=#{1}")
	void modifyTelephoneByUserId(String telephone, int userId) throws Exception;
	
	@Update("update user set nickname=#{nickname} where id=#{userId}")
	void modifyAccountByUserId(String account, int userId) throws Exception;
	
	@Update("update user_info set icon=#{0} where user_id=#{1}")
	void modifyIconByUserId(String icon, int userId) throws Exception;
	
	@Update("update user set email=#{email} where id=#{userId}")
	void modifyEmailByUserId(String email, int userId) throws Exception;
	
	@Insert("insert into user(password, phone, email, register_time, is_locked) "
			+ "values( #{password}, #{phone}, #{email}, #{registerTime}, #{isLocked})")
	int saveUser(User user) throws Exception;
	
	@Insert("insert into user_info(user_id, sex, nickname, birthday, icon, money, coin, exp) "
			+ "values(#{userId}, #{sex}, #{nickname},#{birthday}, #{icon}, #{money}, #{coin}, #{exp})")
	void saveUserInfo(UserInfo userInfo) throws Exception;
	
	@Insert("insert into user_role values(#{0}, #{1})")
	void relateRole(int userId, int roleType) throws Exception;
	
	@Select("select * from user_info where user_id=#{userId} limit 1")
	UserInfo findUserInfoByUserId(int userId);
	
	@Update("update user_info set nickname=#{nickname},sex=#{sex},birthday=#{birthday},description=#{description} where user_id=#{userId}")
	void updatePersonInfo(UserInfo userInfo) throws Exception;
	
	@Update("update user set is_locked=#{0} where id=#{1}")
	void updateIsLockByUserId(boolean isLocked, int userId)throws Exception;
	
	@Insert("insert into user_permission(user_id, permission) values(#{userId}, #{permission})")
	void saveUserPermission(UserPermission up)throws Exception;
}
